package com.ruoyi.project.hcy.product.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.hcy.product.domain.img.ProImg;
import com.ruoyi.project.hcy.product.domain.product.Product;
import com.ruoyi.project.hcy.product.domain.product.ProductVo;
import com.ruoyi.project.hcy.product.service.IProFavorites;
import com.ruoyi.project.hcy.product.service.IProImgService;
import com.ruoyi.project.hcy.product.service.IProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductViewController
 * @Author SAI
 * @Date 2020/9/20 13:21
 * @Description ProductViewController
 * @Version 1.0
 */
@Controller
@RequestMapping("/hcy/product/productView")
public class ProductViewController extends BaseController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProFavorites proFavorites;



    private String prefix = "hcy/product/view";

    @RequiresPermissions("product:productView:view")
    @GetMapping()
    public String operlog(){
        return prefix+"/productView";
    }

    @GetMapping("/edit/{productId}")
//    @ResponseBody
    public String picStore(ModelMap mmap){
        List<ProductVo> productVos = productService.selectProductList(null);
        mmap.put("productVos",productVos);
        return prefix+"/picStore";
    }

    @PostMapping("/check")
    @ResponseBody
    public AjaxResult addFavorites(Long[] productId){
        if ("1".equals(proFavorites.checkFavoritesUnique(productId))) {
            return error("加入收藏夹失败，产品已存在");
        }
        return toAjax(proFavorites.addFavorites(productId));
    }
}
